package com.huiquan.vocab.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.constants.AnalysisSentenceConstant;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.vocab.service.VocabElementSourceService;

@Controller
public class VocabElementSourceController {

	@Autowired
	private VocabElementSourceService vocabElementSourceService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/vocab/element_xbs_source_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Integer checkFlag = 0;
		String xbsSourceKey = "";
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("checkFlag", checkFlag.toString());
			pageParam.put("exactSearchFlag", "0");
			pageParam.put("xbsSourceKey", AnalysisSentenceConstant.XBS_SOURCE_TYPE_NULL);

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
			String checkFlagStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "checkFlag");
			checkFlag = Integer.parseInt(checkFlagStr);
		}

		// 获取页面参数，如果request中有值则修改session，否则从session中拿值
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String vocabularyKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "vocabularyKey");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");
		xbsSourceKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "xbsSourceKey");
		String propertyKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "propertyKey");

		return vocabElementSourceService.list(start, vocabularyKey, checkFlag, exactSearchFlag, countPerPageStr,
				xbsSourceKey, propertyKey);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_change_restrict_source.do")
	public ReturnData elementChangeRestrictSource(HttpServletRequest req, HttpServletResponse resp) throws Exception {

		String id = req.getParameter("id"); // 小词的id
		String restrictSource = req.getParameter("restrictSource"); // 限定类别

		return vocabElementSourceService.elementChangeRestrictSource(id, restrictSource);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_source_submit_page.do")
	public ReturnData submitPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String ids = req.getParameter("ids");

		return vocabElementSourceService.submitPage(ids);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_source_init.do")
	public ReturnData init(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		return vocabElementSourceService.init();
	}

}
